package com.example.tms.tools;

import com.example.tms.bean.FileInfo;
import com.example.tms.bean.OilcardMgmt;
import com.example.tms.bean.OrderMgmt;
import com.example.tms.bean.ReturnInfo;
import com.example.tms.mapper.FileMapper;
import com.example.tms.mapper.OrderMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Component
public class Schedul {

    private final FileMapper fileMapper;
    private final OrderMapper orderMapper;

    @Autowired
    public Schedul(FileMapper fileMapper, OrderMapper orderMapper){
        this.fileMapper = fileMapper;
        this.orderMapper = orderMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

//    private final static long ONE_Minute =  60 * 1000;
//
//    @Scheduled(fixedDelay=ONE_Minute)
//    public void fixedDelay(){
//        logger.info("fixedDelay执行...");
//    }
//
//    @Scheduled(fixedRate=ONE_Minute)
//    public void fixedRate(){
//        logger.info("fixedRate执行...");
//    }

    //定期清除文件
    @Scheduled(cron="0 0 3 ? * 7")//每周日三点
    public void clearFile(){
        String classPath = this.getClass().getClassLoader().getResource("").getPath();
//        String path = classPath.substring(0,classPath.indexOf(GlobalParam.filesposition))+"files/";
        String path = classPath+"static/files/";
        path=path.replaceAll("%20"," ");
        List<FileInfo> fileInfoList = fileMapper.findBySfcz(false);
        for(FileInfo fileInfo : fileInfoList){
            GlobalParam.deleteF(fileInfo, path);
            fileMapper.delete(fileInfo.getFileid());
        }
        List<OrderMgmt> orderMgmts = orderMapper.findForDelete("待发布");
        for(OrderMgmt orderMgmt:orderMgmts){
            orderMapper.delete(orderMgmt.getDdid());
        }

        logger.info("日期"+GlobalParam.getSRq()+" 3点——清除未提交文件！");
    }

}
